package com.lsh.payment.core.service.refund;

import com.alibaba.fastjson.JSONObject;
import com.lsh.payment.api.model.refund.RefundRequest;
import com.lsh.payment.core.model.payenum.PayChannel;
import com.lsh.payment.core.model.payenum.RefundStatus;
import com.lsh.payment.core.model.refund.PayRefund;
import com.lsh.payment.core.util.Idwork.RandomUtil;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Date;

/**
 * Project Name: lsh-payment
 *
 * @author peter
 * @date 18/7/9
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
public class RefundFactory {

    public static PayRefund payRefundBuilder(RefundRequest refundRequest, PayChannel payChannel){

        PayRefund addPayRefund = new PayRefund();
        addPayRefund.setPaymentRefundNo(RandomUtil.snowFlakeId());
//        addPayRefund.setPayPaymentNo(refundRequest.getPay_payment_no());
        addPayRefund.setCreateAt(new Date());
        addPayRefund.setUpdateAt(new Date());
        addPayRefund.setAmountType("CNY");
        addPayRefund.setRefundAmount(new BigDecimal(refundRequest.getRefund_fee()));
        addPayRefund.setRefundTradeId(refundRequest.getRefund_trade_id());
        addPayRefund.setIsValid(1);
        addPayRefund.setRefundStatus(RefundStatus.REFUND_NEW.getValue());
        addPayRefund.setCallSystem(refundRequest.getSystem());
        addPayRefund.setRefundChannel(payChannel.getName());
        addPayRefund.setRefundNotifyUrl(refundRequest.getNotify_url());

        JSONObject ext = new JSONObject();
        ext.put("notify_ext", StringUtils.isNotBlank(refundRequest.getNotify_ext()) ? refundRequest.getNotify_ext() : "{}");
        ext.put("refund_type", refundRequest.getRefund_type());
        ext.put("uid",refundRequest.getUid());

        addPayRefund.setExt(ext.toJSONString());

        return addPayRefund;
    }
}
